/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.yihaodian.service.impl.YihaodianProductServiceImpl
   Module Description   :

   Date Created      : 2011/12/16
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.yihaodian.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.common.utils.FileUtils;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.product.entity.Specification;
import com.yaodian100.ec.product.service.ProductService;
import com.yaodian100.ec.shelf.entity.Shelf;
import com.yaodian100.ec.shelf.entity.ViewShelf;
import com.yaodian100.ec.shelf.service.ShelfService;
import com.yaodian100.ec.yihaodian.service.YihaodianProductService;
import com.yihaodian.product.entity.ProductMerchant;
import com.yihaodian.product.entity.ProductMerchantRequest;
import com.yihaodian.product.entity.ProductMerchantResponse;
import com.yihaodian.product.entity.ProductSupplier;
import com.yihaodian.product.entity.ProductSupplierRequest;
import com.yihaodian.product.entity.ProductSupplierResponse;
import com.yihaodian.product.remote.ProductMerchantService;
import com.yihaodian.product.remote.ProductSupplierService;

/**
 * @author jeff.ma
 * 
 */
public class YihaodianProductServiceImpl implements YihaodianProductService {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private ProductMerchantService remoteYihaodianProductMerchantService;
	@Autowired
	private ProductSupplierService remoteYihaodianProductSupplierService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private ShelfService shelfService;
	@Autowired
	private ProductService productService;
	@Autowired
	private BaseManager baseManager;
	private String checkCode = "5623-16-7897-55-72713-5071-874118-48-94";

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.yihaodian.service.YihaodianProductService#backup(java.util.Date, java.util.Date)
	 */
	@Override
	public void backupProductMerchant(Date start, Date end) {
		List<Orders> result = new ArrayList();

		if (start == null) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -1);
			start = cal.getTime();
		}
		if (end == null) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(DateUtil.getCurrentTime());
			cal.add(Calendar.MONTH, 1);
			end = cal.getTime();
		}
		ProductMerchantRequest req = new ProductMerchantRequest(checkCode);
		req.setStartDateTime(DateUtil.convertDateToString("yyyyMMddHHmmss", start));
		req.setEndDateTime(DateUtil.convertDateToString("yyyyMMddHHmmss", end));

		try {
			int page = 3;
			Map<Long, ProductMerchant> productMap = new HashMap();
			Map shelfMap = new HashMap();
			Map<String, ViewShelf> tempShelfMap = new HashMap();
			for (int i = 1; i <= page; i++) {
				req.setCurPage(Integer.valueOf(i));
				logger.debug("req:{}", req);
				ProductMerchantResponse res = remoteYihaodianProductMerchantService.getProductMerchant(req);
				// logger.debug("res:{}", res);

				if (res.getProductMerchantList() == null) {
					break;
				}

				for (ProductMerchant p : res.getProductMerchantList()) {
					productMap.put(p.getProductId(), p);
					if (StringUtils.isNotEmpty(p.getOuterId())) {
						Specification s = productService.getSpecification(Long.valueOf(p.getOuterId()));
						if ((s != null) && (tempShelfMap.get(s.getProductId()) == null)) {
							Shelf shelf = shelfService.getActiveShelfByProductId(s.getProductId());
							// logger.debug(s.getProductId() + ":" + shelf.getShelfPrice());
							shelfMap.put(p.getProductId(), shelf);
						} else {
							// by pass
						}
					}
				}
				page = res.getTotal().intValue() / req.getPageRows().intValue();
				if (res.getTotal().intValue() % req.getPageRows().intValue() == 0) {
					page += 1;
				}
			}

			Map objs = new HashMap();
			objs.put("products", productMap.values());
			objs.put("shelfs", shelfMap);
			objs.put("rowSize", productMap.size() + 1);
			String excel = templateService.format("yihaodian/merchantProductToExcel", objs);
			File file = new File("ProductMerchant_" + DateUtil.convertDateToString("yyyyMMdd-HH", new Date()) + ".xls");
			FileUtils.writeStringToFile(file, excel);
		} catch (Exception e) {
			logger.error("备份失败:" + req.getStartDateTime() + "~" + req.getEndDateTime(), e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.yihaodian.service.YihaodianProductService#backupProductSupplier(java.util.Date,
	 * java.util.Date)
	 */
	@Override
	public void backupProductSupplier(Date start, Date end) {
		List<Orders> result = new ArrayList();

		if (start == null) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -1);
			start = cal.getTime();
		}
		if (end == null) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(start);
			cal.add(Calendar.MONTH, 1);
			end = cal.getTime();
		}
		ProductSupplierRequest req = new ProductSupplierRequest(checkCode);
		req.setStartDateTime(DateUtil.convertDateToString("yyyyMMddHHmmss", start));
		req.setEndDateTime(DateUtil.convertDateToString("yyyyMMddHHmmss", end));

		try {
			int page = 1;
			Map productMap = new HashMap();
			Map shelfMap = new HashMap();
			for (int i = 1; i <= page; i++) {
				req.setCurPage(Integer.valueOf(i));
				logger.debug("req:{}", req);
				ProductSupplierResponse res = remoteYihaodianProductSupplierService.getProductSupplier(req);
				logger.debug("res:{}", res);

				if (res.getProductSupplierList() == null) {
					break;
				}

				for (ProductSupplier p : res.getProductSupplierList()) {
					productMap.put(p.getProductId(), p);
				}
				page = res.getTotal().intValue() / req.getPageRows().intValue();
				if (res.getTotal().intValue() % req.getPageRows().intValue() == 0) {
					page += 1;
				}
			}

			File file = new File("ProductSupplier_" + DateUtil.convertDateToString("yyyyMMdd", new Date()) + ".xml");

			FileUtils.writeStringToFile(file, StringUtils.toXML(productMap, true));
		} catch (Exception e) {
			logger.error("备份失败:" + req.getStartDateTime() + "~" + req.getEndDateTime(), e);
		}
	}

}
